package org.example.security;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

@Data
public class UserDetailsImpl implements UserDetails {
    private String password;
    private String username;
    private String status;
    private Set<String>permsSet;
    private Set<String> roleKeySet;

    //返回登录用户权限字符串和角色key的集合
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority>list=new ArrayList<>();
        for (String perms:permsSet){
            list.add(new SimpleGrantedAuthority(perms));
        }
        for (String role:roleKeySet){
            list.add(new SimpleGrantedAuthority("ROLE_"+role));
        }
        return list;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return status.equals("0");
    }
}
